package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.marktotop.entity.business.VaccineHistory;
import com.marktotop.error.SystemCodeEnum;
import com.marktotop.error.SystemException;
import com.marktotop.mapper.VaccineHistoryMapper;
import com.marktotop.service.IVaccineHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Service
public class VaccineHistoryServiceImpl extends ServiceImpl<VaccineHistoryMapper, VaccineHistory> implements IVaccineHistoryService {

    @Autowired
    private VaccineHistoryMapper historyMapper;

    /**
     * 判断是否有无疫苗历史记录
     *
     * @param vaccinatorId
     * @param vaccineId
     * @return 有记录true 无记录false
     */
    @Override
    public boolean haveVaccineRecord(Long vaccinatorId, Long vaccineId) {
        // 每一个人对应每一种疫苗的注射历史表中只有一条记录
        VaccineHistory vaccineHistory = historyMapper.selectOne(
                new QueryWrapper<VaccineHistory>()
                        .eq("vaccinator_id", vaccinatorId)
                        .eq("vaccine_id", vaccineId));
        // 没有记录 - 可以注射
        if (null == vaccineHistory){
            return false;
        }
        return true;
    }


    /**
     * 判断该接种人是否已经接种过该疫苗的对应针数
     *
     * @param vaccinatorId  接种人id
     * @param vaccineId     疫苗id
     * @param vaccineNeedle 预约第几针
     * @return
     * @throws SystemException
     */
    @Override
    public boolean getVaccineRecord(Long vaccinatorId, Long vaccineId, Integer vaccineNeedle) throws SystemException {
        // 每一个人对应每一种疫苗的注射历史表中只有一条记录
        VaccineHistory vaccineHistory = historyMapper.selectOne(
                new QueryWrapper<VaccineHistory>()
                        .eq("vaccinator_id", vaccinatorId)
                        .eq("vaccine_id", vaccineId));
        // 如果疫苗记录中  注射针数 >= 预约针数，不能在进行注射该种疫苗
        if (vaccineHistory.getInjectionCount() >= vaccineNeedle){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"您已经注射过该疫苗的第"+vaccineHistory.getInjectionCount()+"针，不能重复注射!");
        } else {
            return true;
        }
    }


    /**
     * 返回该接种人的当前疫苗接种记录
     *
     * @param vaccinatorId
     * @param vaccNumber
     * @return
     * @throws SystemException
     */
    @Override
    public VaccineHistory getVaccineRecord(Long vaccinatorId, Long vaccNumber) throws SystemException {
        // 每一个人对应每一种疫苗的注射历史表中只有一条记录
        VaccineHistory vaccineHistory = historyMapper.selectOne(
                new QueryWrapper<VaccineHistory>()
                        .eq("vaccinator_id", vaccinatorId)
                        .eq("vaccine_id", vaccNumber));
        // 没有记录
        if (null == vaccineHistory){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"您还没有注射过该疫苗,没有查到记录!");
        }
        // 有记录，但记录中注射数为空
        if (null == vaccineHistory.getInjectionCount()){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"接种记录中您已注射过该疫苗,但没有查到接种数!");
        }
        return vaccineHistory;
    }


}
